package com.example.luyao.faceservertest;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

// com.squareup.okhttp3:okhttp:3.12.1
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class test {
    public static final JSONObject parseOkhttp3Response(okhttp3.Response response) {
        int code = response.code();
        //Log.d(TAG, code + response.toString());
        JSONObject responseJson = new JSONObject();
        try {
            if (code == 200) {
                String response_str = response.body().string();
                responseJson = new JSONObject(response_str);
                //Log.d(TAG, responseJson.toString());

            } else {
                String error_str = response.message();
                //error_str = error_str.replaceAll("\"", "\\\\\"");
                responseJson = new JSONObject(error_str);
                //Log.e(TAG, responseJson.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return responseJson;
    }

    private int test_register_user() {
        long startTime = System.currentTimeMillis();

        File image_file = new File("test.jpg");
        if (!image_file.exists()) return 0;
        byte[] image_data = new byte[(int) image_file.length()];
        try {
            BufferedInputStream buf = new BufferedInputStream(new FileInputStream(image_file));
            buf.read(image_data, 0, image_data.length);
            buf.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), image_data);

        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("image", "img.jpg", requestFile)
                .build();
        String url = "http://120.79.161.218:5055" + "/register_user";
        String user_id = "e10adc3949ba59abbe56e057f20f883e";
        String remark = "test";
        String name = "李四";
        url += "?user_id=" + user_id + "&remark=" + remark + "&name=" + name;
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        okhttp3.Call call = client.newCall(request);
        call.enqueue(new okhttp3.Callback() {
            @Override
            public void onResponse(okhttp3.Call call, okhttp3.Response response) {
                JSONObject responseJson = parseOkhttp3Response(response);
                if (response.code() == 200) {
                    System.out.println("test_register_user " + responseJson.toString());
                } else {
                    //toast("连接网络失败，请稍后再试");
                    System.out.println("test_register_user " + responseJson.toString());
                }
                System.out.println("test_register_user spend " + (System.currentTimeMillis() - startTime));
            }

            @Override
            public void onFailure(okhttp3.Call call, IOException e) {
                System.out.println("连接网络失败，请检查您的网络");
                e.printStackTrace();
            }
        });
        return 0;
    }
}
